<?php
/*
 * 本控制器用于分会管理
 *
 * */
namespace App\Http\Controllers\Webapi;

use App\Common\ApiData;
use App\Http\Controllers\Controller;
use App\Models\V1\ChapterModel;
use App\Models\V1\DrpModel;
use App\Models\V1\MemberModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class ChapterController extends Controller{

    private $apiData;
    public function __construct(){

        //parent::__construct();
        $this->apiData  = new ApiData();
    }

    /**
     * 获得分会信息
     * */
    public function chapter_info(Request $request){
        $chapterId = $request->input('chapterId');
        $chapterModel   = new ChapterModel();
        $chapterInfo    = $chapterModel->chapter_info($chapterId);
        return $this->apiData->set_data('chapterInfo',$chapterInfo)->send(200,'ok');
    }

    /**
     * 会长列表
     * */
    public function prisidents(Request $request){
        $chapterId  = $request->input('chapterId',0);
        $keywords   = $request->input('keywords','');
        $chapterModel   = new ChapterModel();
        $prisidents     = $chapterModel->get_chapter_prisidents($chapterId,$keywords,'F');

        $apiData        = new ApiData();
        return $apiData->set_data('prisidents',$prisidents)->send(200,'ok');

    }


    /**
     * 学习官列表
     * */
    public function officers(Request $request){
        $chapterId  = $request->input('chapterId',0);
        $keywords   = $request->input('keywords','');
        $userId     = $request->input('userId',0);
        $type       = $request->input('type');
        $roleType   = $request->input('roleType');

        $chapterModel   = new ChapterModel();
        $chapterInfo    = $chapterModel->chapter_info($chapterId);

        if($type > 0)
            $city = $chapterInfo->city;
        elseif($type < 0)
            $city = -$chapterInfo->city;
        else
            $city = 0;


        if($roleType == 'prisident')
            $officers   = $chapterModel->get_clos_of_chapter($chapterId,$city,$keywords);
        elseif($roleType == 'clo')
            $officers   = $chapterModel->get_clos_of_clo($userId,$keywords);


        $apiData        = new ApiData();
        return $apiData->set_data('officers',$officers)->send(200,'ok');
    }



    /**
     * 会员列表
     * */
    public function members(Request $request){
        $chapterId  = $request->input('chapterId');
        $userId     = $request->input('userId',0);
        $keywords   = $request->input('keywords');
        $type       = $request->input('type');
        $roleType   = $request->input('roleType');

        $chapterModel   = new ChapterModel();
        $chapterInfo    = $chapterModel->chapter_info($chapterId);

        if($type > 0)
            $city = $chapterInfo->city;
        elseif($type < 0)
            $city = -$chapterInfo->city;
        else
            $city = 0;

        if($roleType == 'clo')
            $members = $chapterModel->get_members_of_clo($userId,$keywords);
        else
            $members = $chapterModel->get_members_of_chapter($chapterId,$city,$keywords);
        $apiData    = new ApiData();
        return $apiData->set_data('members',$members)->send(200,'ok');
    }

    /**
     * 公司列表
     * */
    public function companys(Request $request){
        $chapterId  = $request->input('chapterId');
        $userId     = $request->input('userId');
        $keywords   = $request->input('keywords');
        $type       = $request->input('type',0);
        $roleType   = $request->input('roleType');


        $chapterModel   = new ChapterModel();
        $chapterInfo    = $chapterModel->chapter_info($chapterId);

        if($type > 0)
            $city = $chapterInfo->city;
        elseif($type < 0)
            $city = -$chapterInfo->city;
        else
            $city = 0;

        if($roleType == 'clo')
            $companys   = $chapterModel->get_companys_of_clo($userId,$keywords);
        else
            $companys   = $chapterModel->get_companys_of_chapter($chapterId,$city,$keywords);
        $apiData    = new ApiData();
        return $apiData->set_data('companys',$companys)->send(200,'ok');
    }



    /**
     * 企业套餐
     * */
    public function company_packages(){
        $companyPackages = DB::table('company_package')
            ->where('is_delete',0)
            ->orderBy('money')
            ->get();

        return $this->apiData
            ->set_data('packages',$companyPackages)
            ->send(200,"ok");

    }


}